package com.asset.bill.biz.impl;

import com.asset.account.biz.IAccountBiz;
import com.asset.account.form.AccountForm;
import com.asset.bill.biz.IBillBiz;
import com.asset.bill.dao.entity.BillEntity;
import com.asset.bill.dao.mapper.IBillMapper;
import com.asset.bill.enums.EnumTradingDirectionType;
import com.asset.bill.enums.EnumTransactionType;
import com.asset.bill.form.BillForm;
import com.asset.bill.form.BillQueryForm;
import com.asset.common.enums.ResultCodeEnum;
import com.asset.common.helper.CurrentUserHelper;
import com.asset.common.pageform.PageForm;
import com.asset.common.result.ResultMsg;
import com.asset.common.utils.DateUtil;
import com.asset.common.utils.ValidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BillBizImpl implements IBillBiz {

    @Resource
    private IBillMapper ibillMapper;

    @Autowired
    private IAccountBiz userAccountBiz;

    @Override
    public PageForm<BillForm> getBillList(BillQueryForm queyForm) { 
        PageForm<BillForm> resultMsg = new PageForm<BillForm>();
        Long currentUserId = CurrentUserHelper.getCurrentUserId();
        AccountForm userAccountForm = userAccountBiz.getByUserId(currentUserId);

        List<BillEntity> billEntityList = ibillMapper.getUserBillList(userAccountForm.getId(), queyForm.getCurrentPage(), queyForm.getPageSize());
        Integer totalCount = ibillMapper.getTotal(userAccountForm.getId());
        resultMsg.setTotal(totalCount);
        if (CollectionUtils.isEmpty(billEntityList)) {
            return resultMsg;
        }
        List<BillForm> forms = new ArrayList<>();
        for (BillEntity billEntity : new ArrayList<>(billEntityList)) {
            BillForm form = conver2Form(billEntity);
            forms.add(form);
        }
        resultMsg.setDataList(forms);
        return resultMsg;
    }

    @Override
    public ResultMsg updateBill(BillForm billForm) {
        ResultMsg resultMsg = new ResultMsg();
        if (billForm == null) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("入参有误！请确认后重试");
            return resultMsg;
        } else if (billForm.getTransactionAmount() == null || billForm.getTransactionType() == null || billForm.getTradingDirectionType() == null) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("必填项不能为空！请确认后重试");
            return resultMsg;
        }
        Long currentUserId = CurrentUserHelper.getCurrentUserId();
        AccountForm userAccountForm = userAccountBiz.getByUserId(currentUserId);
        if (userAccountForm == null) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("账户信息不存在！请确认后重试");
            return resultMsg;
        }
        BillEntity entity = conver2Entity(billForm, userAccountForm.getId());
        entity.setUserId(currentUserId);
        if (entity.getId() == null) {
            ibillMapper.insert(entity);
            resultMsg.setMsg("添加成功！");
        } else {
            //账单更新会重复更新账户  待解决    TODO
            BillEntity oldBill = ibillMapper.getById(billForm.getId());
            userAccountBiz.synchronizeUserAccountReBack(oldBill.getAccountId(), oldBill.getTransactionAmount(), EnumTradingDirectionType.getEnum(oldBill.getTradingDirectionType()).getCode());
            ibillMapper.update(entity);
            resultMsg.setMsg("修改成功！");
        }
        //同步更新账户信息
        userAccountBiz.synchronizeUserAccount(userAccountForm.getId(), billForm.getTransactionAmount(), billForm.getTradingDirectionType());
        resultMsg.setCode(ResultCodeEnum.SUCCESS.getCode());
        return resultMsg;
    }

    @Override
    public ResultMsg deleteById(Long billId) {
        ResultMsg resultMsg = new ResultMsg();
        if (ValidUtils.isInvalid(billId)) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("入参有误！请确认后重试");
            return resultMsg;
        }
        BillEntity billEntity = getBillById(billId);
        ibillMapper.deleteById(billId);
//        if(EnumTradingDirectionType.PAY.getCode().equals(userBillEntity.getTrading_direction_type())){
//            userAccountBiz.synchronizeUserAccount(userBillEntity.getAccount_id(),userBillEntity.getTransaction_amount(),EnumTradingDirectionType.INCOME.getCode());
//        }else if(EnumTradingDirectionType.INCOME.getCode().equals(userBillEntity.getTrading_direction_type())){
//            userAccountBiz.synchronizeUserAccount(userBillEntity.getAccount_id(),userBillEntity.getTransaction_amount(),EnumTradingDirectionType.PAY.getCode());
//        }
        userAccountBiz.synchronizeUserAccountReBack(billEntity.getAccountId(), billEntity.getTransactionAmount(), EnumTradingDirectionType.getEnum(billEntity.getTradingDirectionType()).getCode());
        resultMsg.setCode(ResultCodeEnum.SUCCESS.getCode());
        resultMsg.setMsg("删除成功");
        return resultMsg;
    }

    @Override
    public BillEntity getBillById(Long billId) {
        if (ValidUtils.isInvalid(billId)) {
            return null;
        }
        return ibillMapper.getUserBillById(billId);
    }

    private BillEntity conver2Entity(BillForm billForm, Long accountId) {
        BillEntity entity = new BillEntity();
        if (billForm.getId() != null) {
            entity.setId(billForm.getId());
            if (billForm.getTransactionTime() != null) {
                entity.setTransactionTime(billForm.getTransactionTime());
            }
        } else {
            if (billForm.getTransactionTime() != null) {
//                entity.setTransaction_time(DateUtil.parsingTime2yyyyMMdd(userBillForm.getTransactionTime()));
                entity.setTransactionTime(billForm.getTransactionTime());
            }
        }
        entity.setAccountId(accountId);
        if (billForm.getTradingDirectionType() != null) {
            entity.setTradingDirectionType(billForm.getTradingDirectionType());
        }
        if (billForm.getTransactionAmount() != null) {
            entity.setTransactionAmount(billForm.getTransactionAmount());
        }
        if (billForm.getTransactionType() != null) {
            entity.setTransactionType(billForm.getTransactionType());
        }
        if (billForm.getRemark() != null) {
            entity.setRemark(billForm.getRemark());
        }
        return entity;
    }

    private BillForm conver2Form(BillEntity entity) {
        if (entity == null) {
            return null;
        }
        BillForm billForm = new BillForm();
        if (entity.getId() != null) {
            billForm.setId(entity.getId());
        }
        if (entity.getAccountId() != null) {
            billForm.setAccountId(entity.getAccountId());
        }
        if (entity.getTradingDirectionType() != null) {
            billForm.setTradingDirectionType(entity.getTradingDirectionType());
            billForm.setTradingDirectionTypeName(EnumTradingDirectionType.getEnum(entity.getTradingDirectionType()).getMessage());
        }
        if (entity.getTransactionAmount() != null) {
            billForm.setTransactionAmount(entity.getTransactionAmount());
        }
        if (entity.getTransactionType() != null) {
            billForm.setTransactionType(entity.getTransactionType());
            billForm.setTransactionTypeName(EnumTransactionType.getEnum(entity.getTransactionType()).getMessage());
        }
        if (entity.getTransactionTime() != null) {
            billForm.setTransactionTime(DateUtil.yyyyMMdd2yyyy_MM_dd(entity.getTransactionTime()));
        }
        if (entity.getRemark() != null) {
            billForm.setRemark(entity.getRemark());
        }
        return billForm;
    }
}
